package org.springblade.modules.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.BuildingElevatorTemplate;
import org.springblade.modules.admin.service.BuildingElevatorTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import org.springblade.modules.admin.entity.BuildingElevatorTemplateRele;
import org.springblade.modules.admin.mapper.BuildingElevatorTemplateReleMapper;
import org.springblade.modules.admin.service.BuildingElevatorTemplateReleService;

@Service
public class BuildingElevatorTemplateReleServiceImpl extends BaseServiceImpl<BuildingElevatorTemplateReleMapper, BuildingElevatorTemplateRele> implements BuildingElevatorTemplateReleService {

	@Autowired
	private BuildingElevatorTemplateService betService;

	@Override
	public String saveRele(Long buildingElevatorTemplateId, List<BuildingElevatorTemplateRele> releList) {

		String releIds = null;

		// 为空则删除所有模板关联的旧数据
		if(ObjectUtil.isEmpty(releList)){
//			super.remove(Wrappers.<BuildingElevatorTemplateRele>lambdaQuery().eq(BuildingElevatorTemplateRele::getTemplateId,buildingElevatorTemplateId));
			return releIds;
		}
		// 更新项目电梯关联结构数据
		List<Long> releIdList = new ArrayList<>();
		Long parentId = 0L;
		for (int i = 0; i < releList.size(); i++) {
			BuildingElevatorTemplateRele entity = releList.get(i);
			int level = i+1;
			BuildingElevatorTemplateRele rele = super.getOne(Wrappers.<BuildingElevatorTemplateRele>lambdaQuery()
				.eq(BuildingElevatorTemplateRele::getTemplateId,buildingElevatorTemplateId)
				.eq(BuildingElevatorTemplateRele::getParentId,parentId)
				.eq(BuildingElevatorTemplateRele::getLevel,level)
				.eq(BuildingElevatorTemplateRele::getTemplateNum,entity.getTemplateNum())
				.eq(BuildingElevatorTemplateRele::getTemplateName,entity.getTemplateName()));
			if(ObjectUtil.isNotEmpty(rele)){
				parentId = rele.getId();
				releIdList.add(rele.getId());
			}else {
				entity.setParentId(parentId);
				entity.setLevel(level);
				entity.setTemplateId(buildingElevatorTemplateId);
				super.save(entity);
				parentId = entity.getId();
				releIdList.add(entity.getId());
			}
		}
		releIds = Func.join(releIdList);
		return releIds;
	}

	@Override
	public String saveRele(Long buildingElevatorTemplateId, String[] releValue) {
		BuildingElevatorTemplate bet = betService.selectBetById(buildingElevatorTemplateId);
		if(ObjectUtil.isEmpty(bet)){
			return null;
		}
		List<BuildingElevatorTemplateRele> releList = new ArrayList<>();
		String[] betNames = Func.toStrArray("/",bet.getName());
		for (int i = 0; i < betNames.length; i++) {
			BuildingElevatorTemplateRele rele = new BuildingElevatorTemplateRele();
			rele.setBuildingId(bet.getBuildingId());
			rele.setTemplateId(buildingElevatorTemplateId);
			rele.setTemplateNum(releValue[i]);
			rele.setTemplateName(betNames[i]);
			releList.add(rele);
		}
		return this.saveRele(buildingElevatorTemplateId,releList);
	}


	@Override
	public List<BuildingElevatorTemplateRele> lazyList(Long buildingId, Long parentId) {
		return baseMapper.lazyList(buildingId, parentId);
	}
}

